// specobj.txt 

// A very simple and important script. When the object is used,
// triggers a hardcoded special encounter

// Cell 0 - 
// Cell 1,2 - A stuff done flag. if non-zero, is smashes
// 3,4,5 - golems ot spawn

beginobjectscript; // talking object

variables;

short spawner = 0;
short last_abil;
short is_active = 0;

body;

beginstate INIT_STATE;
	last_abil = get_current_tick();
	set_obj_act_at_dist(ME,1);
	break;

beginstate DEAD_STATE;
	break;

beginstate START_STATE; 
	if (gf(308,0) > 0) {
		sf(get_memory_cell(1),get_memory_cell(2),2);
		end();
		}
	if (get_sdf(get_memory_cell(1),get_memory_cell(2)) > 2) 
		end();
		
	if (is_active == 0) {
		if (get_nearest_good_char(8) >= 0) {
			is_active = 1;
			print_str_color("A power crystal nearby has sensed your presence. It begins to shoot out sparks.",2);
			play_sound(197);
			}
			else end();
		}
	
	if (which_town() == 32) {
		if ((gf(32,get_memory_cell(2)) == 1) || (gf(32,get_memory_cell(2)) == 2)) {
			if (gf(32,get_memory_cell(2)) == 1)
				sf(32,get_memory_cell(2),0);
				else sf(32,get_memory_cell(2),3);
			run_sparkles_on_object(ME,173,1,4);
			spray_missiles(51,8,8);
			damage_nearby(50,5,2,0);
			play_sound(159);
			}
		}
	if (which_town() == 33) {
		if ((gf(33,get_memory_cell(2)) == 1) || (gf(33,get_memory_cell(2)) == 2)) {
			if (gf(33,get_memory_cell(2)) == 1)
				sf(33,get_memory_cell(2),0);
				else sf(33,get_memory_cell(2),3);
			run_sparkles_on_object(ME,173,1,4);
			spray_missiles(51,8,8);
			damage_nearby(50,5,2,0);
			play_sound(159);
			}
		}
		
	if (tick_difference(last_abil,get_current_tick()) > 0) {
		last_abil = get_current_tick();
		if (spawner > 0)
			spawner = spawner - 1;
		}

	if ((spawner == 0) && (get_nearest_good_char(3) >= 0) && (get_sdf(get_memory_cell(1),get_memory_cell(2)) == 0)) {
		run_sparkles_on_object(ME,174,1,4);
		print_str_color("The crystal flashes with light. The trapdoor opens and several",2);
		spawner = 75 - (difficulty() * 5);
		
		spawn_creature(get_memory_cell(3));
		set_summon_level(get_memory_cell(3),1);
		place_particle_num(get_memory_cell(3),1051,1,8);
		spawn_creature(get_memory_cell(4));
		set_summon_level(get_memory_cell(4),1);
		place_particle_num(get_memory_cell(4),1051,1,8);
		spawn_creature(get_memory_cell(5));
		set_summon_level(get_memory_cell(5),1);
		place_particle_num(get_memory_cell(5),1051,1,8);
		
		if (gf(33,22) == 0) 
			print_str_color("  golems climb out.",2);
		if (gf(33,22) == 1) {
			print_str_color("  golems climb out. They aren't fully charged.",2);
			force_char_status(get_memory_cell(3),5,8);
			force_char_status(get_memory_cell(4),5,8);
			force_char_status(get_memory_cell(5),5,8);
			}
		if (gf(33,22) == 2) {
			print_str_color("  golems climb out. They've been charged with extra energy.",2);
			force_char_status(get_memory_cell(3),2,8);
			force_char_status(get_memory_cell(4),2,8);
			force_char_status(get_memory_cell(5),2,8);
			}
			
		if ((gf(32,18) == 0) && (which_town() == 32)) {
			sf(32,18,1);
			ok_dlog(25,4);
			}
		}
	break;

beginstate RUN_ANIM_STATE;
	if ((get_sdf(get_memory_cell(1),get_memory_cell(2)) == 0) && (get_ran(1,0,100) < 16) && (is_active > 0)) 
		run_sparkles_on_object(ME,100,8,3);
		
		
	break;

beginstate USE_STATE;
	if (get_sdf(get_memory_cell(1),get_memory_cell(2)) > 1) {
		print_str_color("The crystal is now cracked and useless.",2);
		end();
		}
	run_special(get_memory_cell(2));
break;
